<template>
  <div class="proof">
    <NavHeader title="全文总结" />
    <div class="proof-content">
      <div class="content-left">
        <LeftSider
          :id="id"
          title="总结"
          :list="chatlist"
          @transmit="getMessage"
          @del="del"
          @clear="clear"
        />
      </div>
      <div class="content-right">
        <div v-if="typePage == 0" class="right-con">
          <div class="upload-file">
            <div class="upload-file-title">上传文档，立即总结：</div>
            <el-upload
              ref="upload"
              drag
              :action="action"
              :headers="headerObj"
              :auto-upload="false"
              hit=""
              accept=".docx"
            >
              <img
                class="upload-icon"
                src="@/assets/imgs/upload-alt.png"
                alt=""
              />
              <div class="el-upload-text">点击上传或拖拽文件到此处</div>
              <div class="el-upload-type">仅支持word文件小于15M</div>
            </el-upload>
            <div class="el-upload-btn" @click="checkTokens">提交总结</div>
          </div>
          <div class="academic-advertise">
            <div class="academic-advertise__poster">
              <img src="@/assets/imgs/academic-home-01.png" alt="" />
              <span class="academic-advertise__title">自动提取与解析</span>
              <span class="academic-advertise__tips"
                >仅需上传文档，即可让AI全面解读您的论文，进行详细解析，精准提取要点</span
              >
            </div>
            <div class="academic-advertise__poster">
              <img src="@/assets/imgs/academic-home-02.png" alt="" />
              <span class="academic-advertise__title">智能评估</span>
              <span class="academic-advertise__tips"
                >不再为主观性审稿意见和缺乏经验的问题而担忧！我们为您提供标准化、客观的评估，准确反馈</span
              >
            </div>
            <div class="academic-advertise__poster">
              <img src="@/assets/imgs/academic-home-03.png" alt="" />
              <span class="academic-advertise__title">专业改进建议</span>
              <span class="academic-advertise__tips"
                >提供针对性的改进建议，助您更轻松履行审稿人职责，同时提高论文质量</span
              >
            </div>
          </div>
        </div>
        <div v-if="typePage == 1" class="content2">
          <div v-if="repotrContent" class="text">
            <MarkDisplay :markdown="repotrContent" repotr-content-ctrl />
            <!-- <v-md-preview class="content" :text="repotrContent" /> -->
          </div>

          <div v-else><div v-html="repotrContent"></div></div>
          <!-- <div class="pbtn" @click="download">导出</div> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavHeader from "@/components/NavHeader";
import LeftSider from "@/components/LeftSider";
import { fetchData } from "@/utils";
import { delChatHis, getChatHis } from "@/api/translate";
import { getUserToken } from "@/api/api";
import MarkDisplay from "vue-mark-display";

import MarkdownIt from "markdown-it";
import { saveAs } from "file-saver";

export default {
  name: "Proof",
  components: {
    MarkDisplay,
    NavHeader,
    LeftSider,
  },
  props: {
    uploadCallback: {
      type: Function,
      default: new Function(),
    },
  },
  data() {
    return {
      id: 0,
      chatlist: [],
      typePage: 0,
      pagenum: 1,
      cacheFilter: { type: "9" },
      headerObj: {
        authToken: localStorage.getItem("userinfo")
          ? JSON.parse(localStorage.getItem("userinfo")).authToken
          : "",
      },
      isloading: false,
      timer: null,
    };
  },
  computed: {
    action() {
      return `${process.env.VUE_APP_BASE_URL}/chat/uploadsummy`;
    },
  },
  mounted() {
    this.getChatHisList();
  },
  methods: {
    checkTokens() {
      fetchData(getUserToken, 6).then((res) => {
        if (res) {
          this.translate();
        } else {
          this.$message.warning("您的token不足，请充值!");
        }
      });
    },
    translate() {
      this.isloading = true;
      this.$refs.upload.submit();
      //开始轮训判断是否解析完成
      this.typePage = 1;
      this.timer = setInterval(() => {
        this.getChatHisList(true);
      }, 3000);
    },
    getChatHisList(bol) {
      fetchData(
        getChatHis,
        {
          ...this.cacheFilter,
          pagesize: 20,
          pagenum: this.pagenum,
        },
        false
      ).then((res) => {
        this.chatlist = res?.list || [];
        this.pages = res.pages;
        if (bol) {
          let data = this.chatlist[0];
          console.log(data);
          this.repotrContent = data.backtext || "";
          this.reportTitle = data.fromtext;
          this.id = data.id;
          if (this.chatlist.length > 0 && data.syncstatus == "01") {
            this.isloading = false;
            this.src = this.src + data.id;
            clearInterval(this.timer);
          }
        }
      });
    },
    del(data) {
      fetchData(delChatHis, { id: data.id }).then(() => {
        this.$message.success("删除成功!");
        this.pagenum = 1;
        this.getChatHisList();
        this.clear();
      });
    },
    clear() {
      if (this.isloading) return;
      if (this.id == 0) {
        this.$message.warning("当前已是新总结!");
      } else {
        this.id = 0;
        this.repotrContent = "";
        this.reportTitle = "";
        this.typePage = "0";
      }
    },
    getMessage(data) {
      if (this.isloading) return;
      data.syncstatus = "01";
      this.repotrContent = data.backtext || "";
      this.reportTitle = data.fromtext;
      this.id = data.id;
      console.log(data.id);
      this.src = this.src + data.id;
      this.typePage = 1;
    },
    download() {
      var md = new MarkdownIt();
      const convertedHtml = md.render(this.repotrContent);
      console.log(convertedHtml);
      const blob = new Blob([convertedHtml], {
        type: "application/vnd.ms-powerpoint;charset=utf-8",
      });
      saveAs(blob, `${this.reportTitle}.ppt`);
    },
  },
};
</script>

<style lang="less" scoped>
.proof {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  .proof-content {
    flex: 1;
    display: flex;
    overflow: hidden;
    .content-left {
      height: 100%;
    }
    .content-right {
      flex: 1;
      overflow: auto;
      padding: 50px 0 40px;
      box-sizing: border-box;
      .right-con {
        width: 1200px;
        margin: 0 auto;
        .upload-file {
          width: 1200px;
          min-height: 452px;
          background: #ffffff;
          box-shadow: 0 6px 16px 1px #0000000a;
          border-radius: 16px;
          opacity: 1;
          padding: 24px 40px 50px;
          box-sizing: border-box;
          .upload-icon {
            width: 60px;
            height: 60px;
            margin: 60px 0 16px;
          }
          .upload-file-title {
            height: 20px;
            font-size: 14px;
            font-weight: 400;
            color: #141414;
            line-height: 20px;
            margin-bottom: 24px;
          }
          /deep/ .el-upload-dragger {
            width: 1120px;
            height: 260px;
            background: #fafbfc;
            border-radius: 8px;
            margin-bottom: 30px;
          }
          .el-upload-text {
            font-size: 13px;
            font-weight: 400;
            color: #595959;
            line-height: 18px;
            margin-bottom: 10px;
          }
          .el-upload-type {
            font-size: 13px;
            font-weight: 400;
            color: #a1a1a1;
            line-height: 18px;
          }
          .el-upload-btn {
            width: 140px;
            height: 42px;
            line-height: 42px;
            border-radius: 8px;
            background: linear-gradient(219deg, #6468e7 0%, #8a55fe 100%);
            transition: all 0.3s ease-in-out;
            cursor: pointer;
            text-align: center;
            color: #fff;
            font-size: 14px;
            margin: 20px auto;
            &:hover {
              box-shadow: 2px 4px 10px 1px #00000029;
            }
          }
        }
        .academic-advertise {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 0 130px;
          margin-top: 60px;
          .academic-advertise__poster {
            display: flex;
            flex-direction: column;
            align-items: center;
            img {
              width: 120px;
              height: 90px;
            }
            .academic-advertise__title {
              height: 21px;
              font-size: 15px;
              font-weight: 400;
              color: #141414;
              line-height: 20px;
              margin-bottom: 16px;
              margin-top: 3px;
            }
            .academic-advertise__tips {
              font-size: 12px;
              font-weight: 400;
              color: #595959;
              line-height: 22px;
              width: 230px;
              text-align: center;
              display: inline-block;
            }
          }
        }
      }
      .content2 {
        background-color: #fff;
        width: 960px;
        margin: 0 auto;
        padding: 30px 30px 80px;
        box-sizing: border-box;
        min-height: calc(100vh - 54px);
        position: relative;
        .title {
          text-align: center;
          font-size: 30px;
          font-weight: 400;
          color: #3f4654;
          line-height: 42px;
          margin-top: 130px;
        }
        .text {
          font-size: 14px;
          line-height: 28px;
          color: #141414;
          margin-top: 60px;
          ::v-deep .mark-display {
            width: 100% !important;
          }
          ::v-deep .slide {
            width: 100%;
          }
        }
        .pbtn {
          position: absolute;
          top: 60px;
          right: 40px;
          border: none;
          outline: none;
          width: 132px;
          height: 36px;
          line-height: 36px;
          border-radius: 6px;
          text-align: center;
          color: #fff;
          background-color: #6965ea;
          cursor: pointer;
          font-size: 13px;
        }
      }
    }
  }
}
</style>
